﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Xml;
using System.Web;


namespace AdventureWorks.DashboardGroep6.Content {
    public partial class report : System.Web.UI.Page {
       private List<String> values = new List<String>();
       static String reportName = "";
       //public ReportService reportService = new ReportService();
       //public AbstractWebservice.AbstractService reportService;
       //groep7report.MonthOverviewShippingMethodGroep7 reportService = new groep7report.MonthOverviewShippingMethodGroep7();

        public List<Component> componentList = new List<Component>();

        protected void Page_Load(object sender, EventArgs e) {
            this.createModule();
        }

        public void setReportService(String reportServiceName)
        {
                    reportName = reportServiceName;
        }


        public void createModule() {
            List<String> productLineList = new List<string>();

            DataSet dataSet = new DataSet();
            switch (reportName)
            {
                case "rapport productie lijn":
                   reportService.AverageProductLifecycle reportService = new reportService.AverageProductLifecycle();
                   dataSet = reportService.getComponents();

                    break;

                case "rapport groep 8":
                   groep8report.ReportService reportService2 = new groep8report.ReportService();
                   dataSet = reportService2.getComponents();
                    break;

                case "rapport Month Overview Shipping Method":
                    groep7report.MonthOverviewShippingMethodGroep7 reportService3 = new groep7report.MonthOverviewShippingMethodGroep7();
                    dataSet = reportService3.getComponents();
                    break;

                default:
                    reportService.AverageProductLifecycle reportService4 = new reportService.AverageProductLifecycle();
                    dataSet = reportService4.getComponents();
                    break;
            }
            
            DataTable dataTable = dataSet.Tables[0];
            DataRowCollection dataRowCollection = dataTable.Rows;

            foreach (DataRow row in dataRowCollection) {
                Component component = new Component();
                component.Id = (Int32)row[dataTable.Columns["component_Id"]];
                component.Label = row[dataTable.Columns["label"]].ToString();
                component.Type = row[dataTable.Columns["type"]].ToString();
                this.componentList.Add(component);
            }

            int rowCount = dataSet.Tables["item"].Rows.Count;
            for (int i = 0; i < rowCount; i++) {
                productLineList.Add(dataSet.Tables["item"].Rows[i]["value"].ToString());
                //productLineList.Add(productLinesDictionary[dataSet.Tables["item"].Rows[i]["value"].ToString()]);
            }

            foreach (Component component in this.componentList) {
                Panel panel = new Panel();
                panel.BackColor = Color.Orange;
                panel.Width = 250;

                String componentType = component.Type;
                switch (componentType) {
                    case "label":
                        addLabel(panel, component.Label);
                        break;
                    case "string":
                        addLabel(panel, component.Label);
                        addTextBox(panel, component.Id);
                        break;
                    case "list":
                        addLabel(panel, component.Label);
                        addDropDownBox(panel, productLineList, component.Id);
                        break;
                    case "datetime":
                        addLabel(panel, component.Label);
                        addCalendar(panel, component.Id);
                        break;
                    default:
                        break;
                }

                div_reportcontent.Controls.Add(panel);

            }
        }

        public void addLabel(Panel panel, String stringLabel) {
            Label label = new Label();
            label.Text = stringLabel;

            panel.Controls.Add(label);
        }

        public void addTextBox(Panel panel, int componentId) {
            TextBox textbox = new TextBox();
            textbox.ID = componentId.ToString();

            panel.Controls.Add(textbox);
        }

        public void addDropDownBox(Panel panel, List<String> productLineList, int componentId) {
            DropDownList dropDownList = new DropDownList();
            dropDownList.ID = componentId.ToString();
            dropDownList.DataSource = productLineList;
            dropDownList.DataBind();

            panel.Controls.Add(dropDownList);
        }

        public void addCalendar(Panel panel, int componentId) {
            Calendar calendar = new Calendar();
            calendar.SelectedDate = DateTime.Now;
            calendar.ID = componentId.ToString();

            panel.Controls.Add(calendar);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            foreach (Component component in this.componentList)
            {
                String id = component.Id.ToString();

                //Control control = this.Page.Form.FindControl(id);
                //Control control = this.FindControl(id);
                ContentPlaceHolder main = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");

                Control control = main.FindControl(id);
                if (control != null)
                {
                    switch (control.GetType().ToString())
                    {
                        default:
                            Response.Write(control.ToString());
                            break;

                        //dropdown
                        case "System.Web.UI.WebControls.DropDownList":
                            DropDownList dropDownList = (DropDownList)main.FindControl(id);
                            this.values.Add(dropDownList.SelectedValue);
                            break;

                        //textbox
                        case "System.Web.UI.WebControls.TextBox":
                            TextBox textBox = (TextBox)main.FindControl(id);
                            this.values.Add(textBox.Text);
                            break;

                        //calendar 
                        case "System.Web.UI.WebControls.Calendar":
                            Calendar calendar = (Calendar)main.FindControl(id);
                            this.values.Add(calendar.SelectedDate.ToString());
                            break;
                    }
                }
            }

            sendVariables();
        }

        private void sendVariables()
        {
            DataSet dataSet = new DataSet();
            XmlDocument xmlDoc = new XmlDocument();

            //Build XML
            XmlElement components = xmlDoc.CreateElement("root");
            xmlDoc.AppendChild(components);

            // add report level
            XmlElement levelsElement = xmlDoc.CreateElement("level");
            components.AppendChild(levelsElement);
            XmlElement levelElement = xmlDoc.CreateElement("value");
            XmlText level = xmlDoc.CreateTextNode("1");
            levelsElement.AppendChild(levelElement);
            levelsElement.LastChild.AppendChild(level);

            // add values
            XmlElement valuesElement = xmlDoc.CreateElement("values");
            components.AppendChild(valuesElement);

            foreach (String value in this.values)
            {
                XmlElement valueElement = xmlDoc.CreateElement("value");
                XmlText txt = xmlDoc.CreateTextNode(value);

                valuesElement.AppendChild(valueElement);
                valuesElement.LastChild.AppendChild(txt);
            }

            //save xml somewhere for debugging
            try
            {
                xmlDoc.Save(@"C:\sendVariables.xml");
            }
            catch
            {
                //nothing
            }

            //turn xml into dataset
            dataSet.ReadXml(new XmlNodeReader(xmlDoc));

            // get report contents from reportservice
            //displayReport(this.reportService.getDataset(dataSet));
            DataSet ds2 = new DataSet();
            switch (reportName)
            {
                case "rapport productie lijn":
                    reportService.AverageProductLifecycle reportService = new reportService.AverageProductLifecycle();
                    ds2 = reportService.getDataset(dataSet);
                    GridView1.DataSource = reportService.getDataset(dataSet);

                    break;

                case "rapport groep 8":
                    groep8report.ReportService reportService1 = new groep8report.ReportService();
                    ds2 = reportService1.getDataset(dataSet);
                    GridView1.DataSource = reportService1.getDataset(dataSet);
                    break;

                case "rapport Month Overview Shipping Method":
                    groep7report.MonthOverviewShippingMethodGroep7 reportService2 = new groep7report.MonthOverviewShippingMethodGroep7();
                    ds2 = reportService2.getDataset(dataSet);
                    GridView1.DataSource = reportService2.getDataset(dataSet);
                    break;

                default:
                    reportService.AverageProductLifecycle reportService4 = new reportService.AverageProductLifecycle();
                    ds2 = reportService4.getDataset(dataSet);
                    GridView1.DataSource = reportService4.getDataset(dataSet);
                    break;
            }
            GridView1.DataBind();
        }
    }
}
